<?php
/*  法规的录入功能
*/
class ClassifyAction extends Action {
  function _initialize() {

  }

  public function index() {
//    $m = M('ALaw');
//    import("@.ORG.Page"); //导入分页类
//    $count = $m->where('hy_id = 0')->count();
//    $records = 10;//每页多少条记录
//    $p = new Page ($count,$records);
//    $p->setConfig('header','篇记录');
//    $p->setConfig('prev',"上一页");
//    $p->setConfig('next','下一页');
//    $p->setConfig('first','<<');
//    $p->setConfig('last','>>');
//    $page = $p->show ();
//    $list = $m->field('id,ltitle,luser,lnumber,ldate_pub')->where('hy_id = 0')->limit($p->firstRow.','.$p->listRows)->order("id")->findAll();
//    $this->assign("list", $list);
//    $this->assign("nowPage",$p->nowPage);
//    $this->assign("page", $page );
//    $this->assign("list", $list );
//    $this->assign("records",$records);
    $this->display();
  }

  public function search() {
    $_search_ = array(
            classify => array(
                    id => array(1=>'hy_id',2=>'zy_id',3=>'dq_id',4=>"lawtype_id"),
                    name => array(1=>'行业',2=>'专业',3=>'地区',4=>"法规类型"),
                    table => array(1=>'a_hy',2=>'a_zy',3=>'a_region',4=>"a_lawtype")
            ),
            name_id => array(1=>'ltitle',2=>'lcontent',3=>'luser',4=>'lnumber'),
            condition => array(
                    key => array('and'=>'and','or'=>'or','not'=>'and'),
                    like => array('and'=>'like','or'=>'like','not'=>'not like')
            ),
    );
    /*  --------------------------------------获取对应的记录------------------------------------------------------------  */
    $sql = "";
    $where = $_search_["classify"]['id'][$_POST['classify']]." = 0 ";
    $search = array(1=>$_POST['ltitle_search'],2=>$_POST['lcontent_search'],3=>$_POST['luser_search'],4=>$_POST['lnumber_search']);
    $condition = array(1=>'and',2=>$_POST['condition1'],3=>$_POST['condition2'],4=>$_POST['condition3']);
    foreach ($search as $key => $val) {
      if ($val == '') continue;
      $valArr = explode('||', $val);
      $where .= " ".$_search_['condition']['key'][$condition[$key]]." (";
      $like = $_search_['condition']['like'][$condition[$key]];
      foreach ($valArr as $v) {
        $where .= " ".$_search_["name_id"][$key]." $like '%".$v."%' or" ;
      }
      $len = strlen($where);
      $where = substr($where,0,$len-2);
      $where .= ")";
    }
    $sql = "select id,ltitle,luser,lnumber,ldate_pub from a_law where 1 and ".$where;
    $m = M('ALaw');
    $count = $m->where($where)->count();
    $size = 100;
    $list = array();
    if ($count > 0) {
      $page = $_GET['page']>0?$_GET['page']:1;
      $sql .= " order by id limit ".($page-1)*$size.",".$size;
      $list = $m->query($sql);
    } else {
      $count = 0;
      $page = 0;
    }
    $rt['list'] = $list;
    $rt['page'] = $page;
    $rt['count'] = $count;
    $rt['size'] = $size;

    /*--------------------------------获取对应的行业，专业，地区分类信息----------------------------------------------*/
    $classify = array();
    $table = $_search_['classify']['table'][$_POST['classify']];
    if ($table == "a_lawtype") {
      $sql = "select id,Laytype_name as title from a_lawtype";
      $classify = $m->query($sql);
    }else {
      $sql = "select t1.id,t1.title,t2.id as did,t2.title as dtitle from $table t1 left join $table t2 on (t1.did=t2.id) ";
      $list = $m->query($sql);
      foreach ($list as $key => $value) {
        if ($value["did"] == 0) {
          $classify[] = array(id=>$value['id'],title=>$value['title'],downLine=>array());
        }
      }
      foreach ($classify as $key => $value) {
        foreach ($list as $k => $v) {
          if ($value['id'] == $v['did']) $classify[$key]["downLine"][] = array(id=>$v['id'],title=>$v['title']);
        }
      }
    }

    $rt['classify'] = $classify;
    $rt['classify_id'] = $_search_["classify"]['id'][$_POST['classify']];
    $this->ajaxReturn($rt,"成功",1);
  }

  public function classifyInsert() {
    $cid = $_POST['cid'];
    $classifyID = $_POST['classify_id'];
    $lids = implode(',', $_POST['lids']);
    $sql = "update a_law set $classifyID = $cid where id in ($lids)";
    $m = M('');
    $m->query($sql);
    $this->ajaxReturn($_REQUEST,"成功",1);
  }

  public function classifyDelete(){
    $lids = $_POST["lids"];
    $lids = implode(',', $lids);
    $m = M();
    $sql = "delete from a_law where id in ($lids)";
    $m->query($sql);
    $this->ajaxReturn('',"成功",1);
  }

  public function content() {
    $m = M('');
    $sql = "select * from a_law where id = ".$_GET['id'];
    $rt = $m->query($sql);
    $this->assign("info",$rt[0]);
    $this->display();
  }
}
?>